$(document).ready(function(){
	$.ajax({
		"type":"get",
		"url":'../PermissionInfoServlet?method=GetPermissionsList',
		"data":{
			"pagenum":1
		},
		success:function(data){
			console.log(data);
		    appendPermissionInfo(data);
				
		},
		error:function(){
			console.log("error");
		}
	});
//	查询按钮事件方法
	$("#searchbtn").click(function(){
		var permissionname = $("#permissionname_ipt").val();
		var permissiontype = $("#permissiontype_ipt").val();
		var permissioncode = $("#permissioncode_ipt").val();
		var per_detail = $("#per_detail_ipt").val();
		$.ajax({
			"type":"post",
			"url":"../PermissionInfoServlet?method=Query",
			"data":{
				"permissionname":permissionname,
				"permissiontype":permissiontype,
				"permissioncode":permissioncode,
				"per_detail":per_detail,
				"pagenum":1
			},
			success:function(data){
//				移除原有的表格  重新生成
				$("tbody").remove();
				$("#pagetoolBar").empty();
				appendPermissionInfo(data);
			}
		
		});
	});

	
})
//	查看按钮方法  参数 userid
	function edit(id){
		var url = "../WebManagement/editPermission.jsp";
		location.href=url+"?per_id="+id;
	}
//append权限信息  生成表格  查询与查看全部 共用
function appendPermissionInfo(data){
	var recordsTotal = JSON.parse(data).recordsTotal;
	 console.log("recordsTotal:"+recordsTotal);
	 if(recordsTotal == 0){
	    var info = JSON.parse(data).Info;
    	console.log(info);
    	$("#resultinfo").text(info);
    	window.setTimeout(function(){
    		$.blockUI({
    			message:$('#noresult'),
    			css: {
    				padding:"20px",
                    border: "1px",                   // 无边界
                    width:"1000px",                     // 中间框框的宽度
                    top:"40%",                        // 高居中
                    left:"20%"                        // 左居中
               },
    		});
    	},100);
    	$("#res_confirm").click(function(){
    		 $.unblockUI();
    		 location.reload();
    	});
	    }else{
		    appendPermissionResult(data);
		    var url ='../PermissionInfoServlet?method=GetPermissionsList';
			pageMaker(recordsTotal,url,"pagetoolBar","permissionlist");
	    }
}

//翻页方法
function permissionlist(data){
	$("tbody").remove();
	appendPermissionResult(data);
}
//有数据的情况下append数据到结果到页面，生成表格
function appendPermissionResult(data){
	 var PersData = JSON.parse(data).PersInfo;
	    $.each(PersData,function(indexs,values){
	    	
 		var btn = "<td class='center'>"
             +"<button id='"+this._id+"_edit' class='btn btn-info' >"
                 +"<i class='glyphicon glyphicon-edit icon-white'></i>修改</button>&nbsp&nbsp";
	    	var tr = "<tr><td>"+this.permissionname+"</td><td>"
	    			+this.permissiontype+"</td><td>"
	    			+this.permissioncode+"</td><td>"
	    			+this.per_detail;
  
//	    	遍历添加行
	    	var tr = tr + btn;
	    	$("#perInfoTable").append(tr);
//	    	绑定编辑按钮点击事件
	    	var btnedit_id =this.role_detail+"_edit";
	    	$("#"+btnedit_id).click(function(){
		    		console.log(this.id);
		    		var roleid = this.id.split("_");
		    		edit(roleid[0]);
	    	});
	    	
	    });
}